Skip to content

qwen4_exp: default the n-gram hash seed to 1234, and check the constants at load (rebased #1) - #2

Merged
eauchs merged 1 commit into
eauchs:add-qwen4-expfrom
sje397:qwen4-exp-ngram-seed-check
Sep 30, 2026
Merged

eauchs merged 1 commit into
eauchs:add-qwen4-expfrom
sje397:qwen4-exp-ngram-seed-check

Conversation

@sje397

@sje397 sje397 commented Sep 26, 2026

Copy link
Copy Markdown

Supersedes #1 by @beatakouchnir — the same change, re-applied onto da1148f9. #1 is mergeable_state: dirty because da1148f9 reorganised the n-gram shard handling underneath it (_NGRAM_SHARD_RE, both key layouts); nothing in the change was wrong, only its anchors moved. Authorship is preserved on the commit.

Why a guard and not just a default. seed is absent from config.json, so the default is load-bearing: a different seed rebuilds different multipliers and every PLE lookup silently lands on the wrong rows. sanitize now compares the constants rebuilt from the config against the checkpoint's own copies and raises on mismatch, instead of degrading in silence.

Verified against a real checkpoint, because the test cannot. test_qwen4_exp_ngram_constants_are_checked builds the model and compares it to itself, so any self-consistent seed satisfies it — it can pin that a mismatch is rejected, but not that 1234 is the reference's seed. A shipped artifact can:

constants present in Jundot/Qwen3.8-Flash-Next-oQ4e-mtp
  ...layers.1.ple.ple_embedding.layer_multipliers        shape (3,)
  ...layers.1.ple.ple_embedding.ngram_heads_vocab_sizes  shape (16,)
  ...layers.1.ple.ple_embedding.ngram_heads_offsets      shape (16,)

candidate seeds vs the checkpoint's own constants
  seed=0      no       (no layer index matched)
  seed=1234   MATCH    all three constants match, ple_layer_index=0
  seed=1235   no
  seed=617    no

layer_multipliers[:6]
  checkpoint   [23703573157769, 20109073645365, 8052911324071]
  seed=1234    [23703573157769, 20109073645365, 8052911324071]
  seed=0       [4788054244585, 5075510189727, 24189832309785]

That checkpoint is a community oQ4e conversion of this architecture; the constants are small tensors and can be read from a shard header without downloading the weights. So 1234 is not merely plausible — it is the seed written into a shipped artifact, which is the argument for checking it at load rather than trusting the default.

Falsified rather than merely green. Deleting the _check_ngram_constant call fails exactly the new test; setting the default back to 0 also fails it (the assertFalse on a seed-0 model's constants). Both files restored byte-identical afterwards (sha256 checked).

One thing worth knowing before review, unrelated to this patch: tests/test_models.py::test_qwen4_exp is red at da1148f9 — the prefill-vs-stepwise consistency assertion. It is not caused by this change: it fails identically with and without it, on da1148f9, 2196836 and ac83bb4, and it is green at 365170c (the commit that added the model). Measured: max|prefill - stepwise| = 1.07e-3 against a magnitude of 1.20 (~0.09%), so it exceeds atol=1e-4 while still agreeing at 1e-2. That reads as a tolerance that has drifted rather than a wrong result, but the suite is red at head, so it is worth deciding deliberately whether to relax the tolerance or chase the precision loss.

Test status here:

  • test_qwen4_exp_ngram_constants_are_checked — PASS
  • test_qwen4_exp — FAIL (pre-existing, unchanged by this patch)

…nts at load

Re-applied onto da1148f by @sje397: eauchs#1 no longer applies because
da1148f reorganised the n-gram shard handling underneath it. The change is
Beata's; only the anchors moved.

The seed is not in config.json, so the default is load-bearing: a different seed
rebuilds different multipliers and every PLE lookup lands on the wrong rows.

`Model.sanitize` now compares the constants rebuilt from the config against the
checkpoint's own copies and raises on mismatch, so a wrong seed fails loudly
instead of silently reading the wrong rows.

Verified against a real checkpoint, which the test alone cannot do (it constructs
the model, so any self-consistent seed satisfies it): the community oQ4e
conversion of this architecture at Jundot/Qwen3.8-Flash-Next-oQ4e-mtp carries
layer_multipliers (3,), ngram_heads_vocab_sizes (16,) and ngram_heads_offsets
(16,), and all three match the seed-1234 rebuild exactly at ple_layer_index 0.
Candidates 0, 617 and 1235 do not match any layer index. So 1234 is not merely
plausible -- it is the seed written into a shipped checkpoint, which is why the
guard is worth having at load rather than trusting the default.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants